﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using Simhopp.Presenter;
using Simhopp.Model;

namespace Simhopp.GUI
{
    public partial class frmJudgeScreen : Form, InterfaceJudgeScreenView
    {
        
        Socket sck;
        PresenterJudgeScreen pJudgeScreen;
        Listener listener;
        string port;
        public event DelegateSubmitScore EventSubmitScore = null;
        public event DelegateStart EventStart = null;
        public event DelegateStop EventStop = null;


        public frmJudgeScreen(Judge judge)
        {
            try
            {
                InitializeComponent();

                IPHostEntry ipHostInfo = Dns.GetHostEntry("www.fruitcakeserver.com");
                IPAddress ipAddress = ipHostInfo.AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork);
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 888);

                //try
                //{
                    listener = new Listener(0);
                    listener.isJudge = true;
                    listener.SocketAccepted += new Listener.SocketAcceptedHandler(listener_SocketAccepted);
                    sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    sck.Connect(remoteEP);

                //}
                //catch (Exception)
                //{
                    
                //    MessageBox.Show("No Connection to competition Server, please try again later");
                //    this.Close();
                //}
                

                //sck.Connect("127.0.0.1", 888);
                pJudgeScreen = new PresenterJudgeScreen(this, judge, listener);
            }
            catch (Exception)
            {
                MessageBox.Show("No connection to Network please try again later");
                this.Close();
            }

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (EventSubmitScore != null)
            {
                EventSubmitScore(trkPoints.Value.ToString(), sck, port);
                btnSubmit.Enabled = false;
                trkPoints.Value = 0;
                trkPoints.Enabled = false;
                lblDiveState.Text = "Waiting for diver...";
                txtDive.Text = "";
                txtDiver.Text = "";
                
            }        
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            CloseForm();
        }

        private void frmJudgeScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (EventStop != null)
                EventStop();

            sck.Close();
            sck.Dispose();
            
        }

        private void CloseForm()
        {

            this.Close();
        }

        private void frmJudgeScreen_Load(object sender, EventArgs e)
        {
            if (EventStart != null && EventSubmitScore != null)
            {
                EventStart();
                port = ((IPEndPoint)listener.socket.LocalEndPoint).Port.ToString();
                EventSubmitScore("", sck, port);
            }
            btnSubmit.Enabled = false;
            
        }

        void listener_SocketAccepted(System.Net.Sockets.Socket e)
        {
            Client client = new Client(e);
            client.Recived += new Client.ClientRecivedHandler(client_Recived);

        }


        void client_Recived(Client sender, byte[] data)
        {
            BeginInvoke((MethodInvoker)delegate
            {
                string[] msg = Encoding.Default.GetString(data).Split('@');
                if (msg[0] != "")
                {
                btnSubmit.Enabled = true;
                trkPoints.Enabled = true;
                lblDiveState.Text = "Please report score for the dive";
                    txtDive.Text = msg[2];
                    txtDiver.Text = msg[1];
                }
            });

            
        }
    }
}
